package com.ctg.itrdc.imageconsole.control.ssh.utils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctg.itrdc.imageconsole.control.ssh.model.SessionOutput;
import com.ctg.itrdc.imageconsole.control.ssh.model.UserSessionsOutput;

/**
 * Utility to is used to store the output for a session until the ajax call that
 * brings it to the screen
 */
public class SessionOutputUtil {

	private static Logger log = LoggerFactory.getLogger(SessionOutputUtil.class);

	private static Map<Long, UserSessionsOutput> userSessionsOutputMap = new ConcurrentHashMap<>();

	private SessionOutputUtil() {
	}

	/**
	 * removes session for user session
	 *
	 * @param sessionId
	 *            session id
	 */
	public static void removeUserSession(Long sessionId) {
		UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId);
		if (userSessionsOutput != null) {
			userSessionsOutput.getSessionOutputMap().clear();
		}
		userSessionsOutputMap.remove(sessionId);
	}

	/**
	 * removes session output for host system
	 *
	 * @param sessionId
	 *            session id
	 * @param instanceId
	 *            id of host system instance
	 */
	public static void removeOutput(Long sessionId, Integer instanceId) {

		UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId);
		if (userSessionsOutput != null) {
			userSessionsOutput.getSessionOutputMap().remove(instanceId);
		}
	}

	/**
	 * adds a new output
	 *
	 * @param sessionOutput
	 *            session output object
	 */
	public static void addOutput(SessionOutput sessionOutput) {
		UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionOutput.getSessionId());
		if (userSessionsOutput == null) {
			userSessionsOutputMap.put(sessionOutput.getSessionId(), new UserSessionsOutput());
			userSessionsOutput = userSessionsOutputMap.get(sessionOutput.getSessionId());
		}
		userSessionsOutput.getSessionOutputMap().put(sessionOutput.getInstanceId(), sessionOutput);
	}

	/**
	 * adds a new output
	 *
	 * @param sessionId
	 *            session id
	 * @param instanceId
	 *            id of host system instance
	 * @param value
	 *            Array that is the source of characters
	 * @param offset
	 *            The initial offset
	 * @param count
	 *            The length
	 */
	public static void addToOutput(Long sessionId, Integer instanceId, char value[], int offset, int count) {
		UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId);
		if (userSessionsOutput != null) {
			userSessionsOutput.getSessionOutputMap().get(instanceId).getOutput().append(value, offset, count);
		}
	}

	/**
	 * returns list of output lines
	 *
	 * @param sessionId
	 *            session id object
	 * @return session output list
	 */
	public static List<SessionOutput> getOutput(Connection con, Long sessionId) {
		List<SessionOutput> outputList = new ArrayList<>();

		UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId);
		if (userSessionsOutput != null) {
			for (Integer key : userSessionsOutput.getSessionOutputMap().keySet()) {
				// get output chars and set to output
				try {
					SessionOutput sessionOutput = userSessionsOutput.getSessionOutputMap().get(key);
					if (sessionOutput != null && sessionOutput.getOutput() != null
							&& StringUtils.isNotEmpty(sessionOutput.getOutput())) {

						outputList.add(sessionOutput);
						userSessionsOutput.getSessionOutputMap().put(key, new SessionOutput(sessionId, sessionOutput));
					}
				} catch (Exception ex) {
					log.error(ex.toString(), ex);
				}
			}
		}
		return outputList;
	}

}
